package com.example.composableexample.lazycolumn

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Image
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composableexample.R


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun LazyColumnScreen() {
    val current = LocalContext.current
    val itemArray = current.resources.getStringArray(R.array.car_array)
    val groupByItems = itemArray.groupBy { it.first() }
    LazyColumn {
        groupByItems.forEach { (manufacturer, models) ->
            stickyHeader {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.padding(start = 8.dp, top = 8.dp)
                ) {
                    Text(
                        text = manufacturer.toString(),
                        color = Color.White,
                        fontSize = 16.sp,
                        modifier = Modifier
                            .background(
                                color = Color(0xFF1DE9B6),
                                shape = RoundedCornerShape(90.dp)
                            )
                            .padding(start = 16.dp, end = 16.dp)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Divider(modifier = Modifier.padding(end = 8.dp))
                }
            }
            items(itemArray) { carName ->
                CarListItem(carName = carName)
            }
        }
    }

}

@Composable
fun CarListItem(carName: String) {
    Row(
        modifier = Modifier
            .padding(4.dp)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(imageVector = Icons.Default.Image, contentDescription = "")
        Spacer(modifier = Modifier.width(4.dp))
        Text(
            text = carName,
            style = MaterialTheme.typography.titleLarge,
            modifier = Modifier.padding(8.dp)
        )
    }
}